package ua.kharkov.khpi.tarasenko.bach.core.utils;

import java.util.StringTokenizer;

import ua.kharkov.khpi.tarasenko.bach.analysis.tnr.RuleG;

public final class RuleUtils {

	private RuleUtils() {
	}

	/**
	 * Returns coma-separated list of ids in antecedent. Important! List ends
	 * with coma!
	 * 
	 * @param rule
	 * @return
	 */
	public static String getAntecedent(RuleG rule) {
		String antec = idsArrayToString(rule.getItemset1());
		return antec;
	}

	/**
	 * Returns coma-separated list of ids in consequent. Important! List ends
	 * with coma!
	 * 
	 * @param rule
	 * @return
	 */
	public static String getConsequent(RuleG rule) {
		String cons = idsArrayToString(rule.getItemset2());
		return cons;
	}

	public static Integer[] parseRuleIdsList(String ids) {
		StringTokenizer st = new StringTokenizer(ids, ",. ");
		Integer[] idList = new Integer[st.countTokens()];

		int index = 0;
		while (st.hasMoreTokens()) {
			idList[index] = Integer.valueOf(st.nextToken());
			index++;
		}
		return idList;
	}

	public static String idsArrayToString(Integer[] ids) {
		String idsList = "";
		for (Integer id : ids) {
			idsList = idsList + id + ",";
		}
		return idsList;
	}
}